{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(1.00000e-33 *\n",
       "       [[ 0.0000,  0.0000,  0.0000],\n",
       "        [ 0.0000,  0.0000,  0.0000],\n",
       "        [ 8.3926,  0.0000,  0.0000],\n",
       "        [ 0.0000,  0.0000,  0.0000],\n",
       "        [ 8.4268,  0.0000,  0.0000]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.Tensor(5,3)   #tensor can be seen as a numpy array or a multidimensional array\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([5, 3])\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand(5,3) # during [0,1],uniform initialization the tensor\n",
    "print(x.size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()[1],x.size()[0] # view the row and column about the tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.4374,  0.1779,  0.2830],\n",
       "         [ 0.0504,  0.5062,  0.2361],\n",
       "         [ 0.5039,  0.5979,  0.3655],\n",
       "         [ 0.4720,  0.4378,  0.4479],\n",
       "         [ 0.1218,  0.0749,  0.1958]]), tensor([[ 0.4374,  0.1779,  0.2830],\n",
       "         [ 0.0504,  0.5062,  0.2361],\n",
       "         [ 0.5039,  0.5979,  0.3655],\n",
       "         [ 0.4720,  0.4378,  0.4479],\n",
       "         [ 0.1218,  0.0749,  0.1958]]), tensor([[ 0.4374,  0.1779,  0.2830],\n",
       "         [ 0.0504,  0.5062,  0.2361],\n",
       "         [ 0.5039,  0.5979,  0.3655],\n",
       "         [ 0.4720,  0.4378,  0.4479],\n",
       "         [ 0.1218,  0.0749,  0.1958]]), tensor([[ 0.4374,  0.1779,  0.2830],\n",
       "         [ 0.0504,  0.5062,  0.2361],\n",
       "         [ 0.5039,  0.5979,  0.3655],\n",
       "         [ 0.4720,  0.4378,  0.4479],\n",
       "         [ 0.1218,  0.0749,  0.1958]]))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# addition ways\n",
    "y = torch.Tensor(5,3)\n",
    "result = torch.Tensor(5,3)\n",
    "torch.add(x,y,out = result)\n",
    "x+y,torch.add(x,y),result,x.add(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.4374,  0.1779,  0.2830],\n",
       "        [ 0.0504,  0.5062,  0.2361],\n",
       "        [ 0.5039,  0.5979,  0.3655],\n",
       "        [ 0.4720,  0.4378,  0.4479],\n",
       "        [ 0.1218,  0.0749,  0.1958]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.add_(y) #this will change the x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.43739486 0.17794251 0.28295857]\n",
      " [0.05037767 0.5062108  0.23614001]\n",
      " [0.50391096 0.59793115 0.36553937]\n",
      " [0.47195226 0.43781447 0.44789994]\n",
      " [0.12182218 0.07490569 0.19577616]]\n"
     ]
    }
   ],
   "source": [
    "z = x.numpy()  # tensor->numpy\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.4374,  0.1779,  0.2830],\n",
      "        [ 0.0504,  0.5062,  0.2361],\n",
      "        [ 0.5039,  0.5979,  0.3655],\n",
      "        [ 0.4720,  0.4378,  0.4479],\n",
      "        [ 0.1218,  0.0749,  0.1958]])\n"
     ]
    }
   ],
   "source": [
    "z=torch.from_numpy(z) # numpy->tensor\n",
    "print(z)\n",
    "# please remember the tensor and numpy share the memory space."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Autograd.\n",
    "from torch.autograd import Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  1.],\n",
       "        [ 1.,  1.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.ones(2,2)\n",
    "x = Variable(x,requires_grad=True)  # tensor->variable\n",
    "y = x.sum()\n",
    "y.backward()  #back-propagation\n",
    "x.grad # Calculate the grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 2.,  2.],\n",
      "        [ 2.,  2.]])\n",
      "tensor([[ 0.,  0.],\n",
      "        [ 0.,  0.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<SumBackward0 at 0x1db0cb627f0>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.backward()\n",
    "print(x.grad)\n",
    "x.grad.data.zero_()  #clear the grad zero\n",
    "print(x.grad)\n",
    "y.grad_fn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = x.data  # variable->tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#neural network nn\n",
    "import torch.nn as nn  \n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Net(\n",
      "  (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))\n",
      "  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))\n",
      "  (fc1): Linear(in_features=400, out_features=120, bias=True)\n",
      "  (fc2): Linear(in_features=120, out_features=84, bias=True)\n",
      "  (fc3): Linear(in_features=84, out_features=10, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "class Net(nn.Module):  #create a class which is nn.Module's derived class\n",
    "    def __init__(self):  # constructor\n",
    "        super(Net,self).__init__()  # father class's constructor\n",
    "        self.conv1 = nn.Conv2d(1, 6, 5) #first convolution layer\n",
    "        self.conv2 = nn.Conv2d(6, 16, 5) #second convolution layer\n",
    "        self.fc1 = nn.Linear(16*5*5, 120) #full connection  layer\n",
    "        self.fc2 = nn.Linear(120, 84) #full connection  layer        \n",
    "        self.fc3 = nn.Linear(84, 10) #full connection  layer\n",
    "    def forward(self, x):\n",
    "        x = F.max_pool2d(F.relu(self.conv1(x)), (2,2))  # convolution ->  activate -> pooling\n",
    "        x = F.max_pool2d(F.relu(self.conv2(x)), 2)  # convolution ->  activate -> pooling\n",
    "        x = x.view(x.size()[0], -1) #reshape the size\n",
    "        x = F.relu(self.fc1(x))  #  active fuction + linear layer\n",
    "        x = F.relu(self.fc2(x)) # active fuction + linear layer\n",
    "        x = self.fc3(x)  # output\n",
    "        return x\n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
